﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XsClient.Common;

namespace XsClient.Parsers
{
    /// <summary>
    /// ResolverFactory
    /// 创建人：zhangyang
    /// 创建时间：2020/6/9 13:49:28
    /// </summary>
    public class ResolverFactory
    {
        private static Dictionary<int, BaseResolver> besolvers = new Dictionary<int, BaseResolver>();
        static ResolverFactory()
        {
            try
            {
                var bqg = new BqgResolver();
                var xbqg = new XBqgResolver();
                var dd = new DingdianResolver();
                besolvers.Add(bqg.Source.ID, bqg);
                besolvers.Add(xbqg.Source.ID, xbqg);
                besolvers.Add(dd.Source.ID, dd);

                _Sources = new List<XsSource>();
                _Sources.Add(bqg.Source);
                _Sources.Add(xbqg.Source);
                _Sources.Add(dd.Source);
            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }
        /// <summary>
        /// 获取解析工具
        /// </summary>
        /// <returns></returns>
        public static BaseResolver GetResolver(int sourceID = 0)
        {
            if (sourceID < 0)
            {
                sourceID = XsConfig.Current.DefaultID;
            }
            if (sourceID > 0)
            {
                if (besolvers.ContainsKey(sourceID))
                    return besolvers[sourceID];

                switch (sourceID)
                {
                    case 1:
                        return new BqgResolver();
                    case 2:
                        return new XBqgResolver();
                    case 3:
                        return new DingdianResolver();
                    default:
                        return new XBqgResolver();

                }
            }
            return new XBqgResolver();
        }

        private static int GetSourceID(string url)
        {
            try
            {
                var sources = XsConfig.Current.Sources;
                foreach (var item in sources)
                {
                    if (url.StartsWith(item.BaseUrl))
                        return item.ID;
                }
            }
            catch { }

            if (url.StartsWith("https://www.xbiquge.la/"))
                return 2;

            if (url.StartsWith("http://www.loubiqu.com/") || url.StartsWith("http://www.biquge.info/") || url.StartsWith("https://www.xbiquwx.la/"))
                return 1;
            if (url.StartsWith("http://www.dingdianxs.la/"))
                return 3;
            return 0;
        }

        /// <summary>
        /// 获取解析工具
        /// </summary>
        /// <returns></returns>
        public static BaseResolver GetResolver(string url)
        {
            return GetResolver(GetSourceID(url.ToLower()));

        }
        private static List<XsSource> _Sources;
        public static List<XsSource> Sources
        {
            get
            {

                return _Sources;
            }
        }
    }
}
